package com.probox.common.entity.service.customer.impl;

import cn.hutool.core.collection.CollectionUtil;
import cn.hutool.core.convert.Convert;
import cn.hutool.core.date.DateUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.probox.common.core.enums.DataStatus;
import com.probox.common.core.utils.SecurityUtils;
import com.probox.common.core.utils.StringUtils;
import com.probox.common.core.web.domain.AjaxResult;
import com.probox.common.entity.domain.customer.CustomerInfoArchives;
import com.probox.common.entity.mapper.customer.CustomerInfoArchivesMapper;
import com.probox.common.entity.service.customer.ICustomerInfoArchivesService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.*;
import java.util.stream.Collectors;

/**
 * 客户档案Service业务层处理
 *
 * @author usoft
 * @date 2022-11-10
 */
@Service
@Transactional
public class CustomerInfoArchivesServiceImpl extends ServiceImpl<CustomerInfoArchivesMapper, CustomerInfoArchives> implements ICustomerInfoArchivesService {
    @Autowired
    private CustomerInfoArchivesMapper customerInfoArchivesMapper;

    /**
     * 根据服务项目和购买记录查询列表
     *
     * @param itemId
     * @param subscribeId
     * @return
     */
    @Override
    public List<Map> listArchivesByItemId(Long itemId, Long subscribeId) {
        List<Map> list = new ArrayList<>();
        QueryWrapper<CustomerInfoArchives> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("data_status", DataStatus.valid.getKey());
        queryWrapper.eq("item_Id", itemId);
        queryWrapper.eq("subscribe_Id", subscribeId);
        List<CustomerInfoArchives> customerInfoArchives = customerInfoArchivesMapper.selectList(queryWrapper);
        return null;

    }

    /**
     * 根据服务项目和预约id查询对应的项目属性 (格子形式展示)
     *
     * @param itemId
     * @param subscribeId
     * @return
     */
    @Override
    public AjaxResult listArchivesLatticeByItemId(Long itemId, Long subscribeId) {
        AjaxResult ajaxResult = new AjaxResult();
        ajaxResult.put("code", 200);
        ajaxResult.put("msg", "查询成功");
        QueryWrapper<CustomerInfoArchives> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("data_status", DataStatus.valid.getKey());
        queryWrapper.eq("item_Id", itemId);
        queryWrapper.eq("subscribe_Id", subscribeId);
        return ajaxResult;

    }


    /**
     * 查询客户档案
     *
     * @param archivesId 客户档案ID
     * @return 客户档案
     */
    @Override
    public CustomerInfoArchives selectCustomerInfoArchivesById(Long archivesId) {
        return this.getById(archivesId);
    }

    /**
     * 查询客户档案列表
     *
     * @param customerInfoArchives 客户档案
     * @return 客户档案
     */
    @Override
    public List<CustomerInfoArchives> queryList(CustomerInfoArchives customerInfoArchives) {
        QueryWrapper<CustomerInfoArchives> queryWrapper = Wrappers.query();
        if (StringUtils.isNotEmpty(String.valueOf(customerInfoArchives.getArchivesId())) && customerInfoArchives.getArchivesId() != null) {
            queryWrapper.eq("archives_id", customerInfoArchives.getArchivesId());
        }
        if (StringUtils.isNotEmpty(String.valueOf(customerInfoArchives.getSubscribeId())) && customerInfoArchives.getSubscribeId() != null) {
            queryWrapper.eq("Subscribe_id", customerInfoArchives.getSubscribeId());
        }
        if (StringUtils.isNotEmpty(String.valueOf(customerInfoArchives.getItemId())) && customerInfoArchives.getItemId() != null) {
            queryWrapper.eq("ITEM_id", customerInfoArchives.getItemId());
        }

        if (StringUtils.isNotEmpty(String.valueOf(customerInfoArchives.getAttributeValue())) && customerInfoArchives.getAttributeValue() != null) {
            queryWrapper.eq("attribute_value", customerInfoArchives.getAttributeValue());
        }
        if (StringUtils.isNotEmpty(String.valueOf(customerInfoArchives.getDataStatus())) && customerInfoArchives.getDataStatus() != null) {
            queryWrapper.eq("DATA_STATUS", customerInfoArchives.getDataStatus());
        }
        if (StringUtils.isNotEmpty(String.valueOf(customerInfoArchives.getCreateUid())) && customerInfoArchives.getCreateUid() != null) {
            queryWrapper.eq("CREATE_UID", customerInfoArchives.getCreateUid());
        }
        if (StringUtils.isNotEmpty(String.valueOf(customerInfoArchives.getCreateName())) && customerInfoArchives.getCreateName() != null) {
            queryWrapper.eq("CREATE_NAME", customerInfoArchives.getCreateName());
        }
        if (StringUtils.isNotEmpty(String.valueOf(customerInfoArchives.getUpdateUid())) && customerInfoArchives.getUpdateUid() != null) {
            queryWrapper.eq("UPDATE_UID", customerInfoArchives.getUpdateUid());
        }
        if (StringUtils.isNotEmpty(String.valueOf(customerInfoArchives.getUpdateName())) && customerInfoArchives.getUpdateName() != null) {
            queryWrapper.eq("UPDATE_NAME", customerInfoArchives.getUpdateName());
        }
        return customerInfoArchivesMapper.selectList(queryWrapper);
    }

    /**
     * 新增客户档案
     *
     * @param customerInfoArchives 客户档案
     * @return 结果
     */
    @Override
    public boolean insertCustomerInfoArchives(CustomerInfoArchives customerInfoArchives) {
        customerInfoArchives.setDataStatus(DataStatus.valid.getKey());
        customerInfoArchives.setCreateName(SecurityUtils.getUsername());
        customerInfoArchives.setCreateTime(DateUtil.date());
        customerInfoArchives.setCreateUid(SecurityUtils.getUserId());
        return this.save(customerInfoArchives);

    }

    /**
     * 修改客户档案
     *
     * @param customerInfoArchives 客户档案
     * @return 结果
     */
    @Override
    public boolean updateCustomerInfoArchives(CustomerInfoArchives customerInfoArchives) {

        return this.updateById(customerInfoArchives);
    }

    /**
     * 批量删除客户档案
     *
     * @param archivesIds 需要删除的客户档案ID
     * @return 结果
     */
    @Override
    public boolean deleteCustomerInfoArchivesByIds(Long[] archivesIds) {
        UpdateWrapper<CustomerInfoArchives> updateWrapper = new UpdateWrapper<>();
        updateWrapper.in("archives_id", Arrays.asList(archivesIds));
        updateWrapper.set("DATA_STATUS", DataStatus.delete.getKey());
        updateWrapper.set("UPDATE_UID", SecurityUtils.getUserId());
        updateWrapper.set("UPDATE_NAME", SecurityUtils.getUsername());
        updateWrapper.set("UPDATE_TIME", DateUtil.date());
        return this.update(updateWrapper);
    }

    /**
     * 批量新增
     *
     * @param list
     */
    @Override
    public void addArchives(List<CustomerInfoArchives> list) {
        if (CollectionUtil.isNotEmpty(list)) {
            list.forEach(vo -> {
                vo.setDataStatus(DataStatus.valid.getKey());
                vo.setCreateName(SecurityUtils.getUsername());
                vo.setCreateTime(DateUtil.date());
                vo.setCreateUid(SecurityUtils.getUserId());
                this.save(vo);
            });
        }
    }
}
